#!/usr/bin/env python
#!coding=utf-8

from PySide.QtCore import QObject,Signal,QTimer
from PySide.QtGui import qApp
from greeevent import *
from greepeer import BaseGreecket,Greepeer,Collaborate
from greenlet import *

class GreeEvent(QObject):
    GreeSignal=Signal(object)
    def __init__(self,parent=None,factory=None):
        QObject.__init__(self)
        self._Factory=factory
        self.GreeSignal.connect(self.eventProccess)

    def eventProccess(self,event):
        self._Factory._eventproccess(event)


class Greecket(BaseGreecket):
    def __init__(self,parent=None,collaborate=False):
        super(Greecket,self).__init__(collaborate)
        self._GreeEvent=GreeEvent(qApp,self)
        self._GreenletLoop=greenlet(self._greenletLoop)
        #由timer循環調用greecket._greenletLoop
        QTimer.singleShot(30,self._eventLoop)
        
    def _eventLoop(self):
        self._GreenletLoop.switch()
        QTimer.singleShot(10,self._eventLoop)

    def pushEvent(self,event=None):
        self._GreeEvent.GreeSignal.emit(event)
        
